package com.xyd.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xyd.entity.Message;
import com.xyd.service.UserService;
import com.xyd.service.impl.UserServiceImpl;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 类描述：
 *
 * @author 张吉哲
 * @date 2021/1/11 11:31
 */
@WebServlet("/usernameIsHasServlet")
public class UsernameIsHasServlet extends HttpServlet {

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    response.setContentType("application/json;charset=utf-8");

    String username = request.getParameter("username");
    UserService userService = new UserServiceImpl();
    boolean flag = userService.isHasUsername(username);

    Message message = new Message();
    message.setFlag(flag);
    if (flag) {
      message.setMessage("用户名重复");
    } else {
      message.setMessage("用户名可用");
    }
    mapper.writeValue(response.getOutputStream(), message);
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doPost(request, response);
  }
}
